package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.utils.ExceptionResult;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.service.IPetstypeService;
import com.example.entity.Petstype;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dengc
 * @since 2023-05-10
 */

@CrossOrigin
@RestController
@RequestMapping("/petstype")
public class PetstypeController {

        @Resource
        private IPetstypeService petstypeService;

        @PostMapping
        public ExceptionResult save(@RequestBody Petstype petstype) {
            return ExceptionResult.success(petstypeService.saveOrUpdate(petstype));
        }

        @DeleteMapping("/{id}")
        public ExceptionResult delete(@PathVariable Integer id) {
            return ExceptionResult.success(petstypeService.removeById(id));
        }

        @GetMapping
        public ExceptionResult findAll() {
            return ExceptionResult.success(petstypeService.list());
        }

        @GetMapping("/{id}")
        public ExceptionResult findOne(@PathVariable Integer id) {
            return ExceptionResult.success(petstypeService.getById(id));
        }

        //批量删除
        @PostMapping("/del/batch")
        public ExceptionResult deleteBatch(@RequestBody List<Integer> ids){
                return ExceptionResult.success(petstypeService.removeByIds(ids));
        }

        //获取宠物种类数量
        @GetMapping("/count/type")
        public ExceptionResult getTypeCount(){
                return ExceptionResult.success(petstypeService.getTypeCount());
        }

        @GetMapping("/page")
        public ExceptionResult findPage(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize) {
                QueryWrapper<Petstype> queryWrapper=new QueryWrapper<>();
                queryWrapper.orderByDesc("id");
                return ExceptionResult.success(petstypeService.page(new Page<>(pageNum, pageSize),queryWrapper));
        }
}
